﻿Public Class ChoreographyUserControl

#Region "Member Fields"
    Private _sequence As NabaztagLib.Choreography.ActivitySequence
#End Region

#Region "Properties"
    Public Property Sequence() As NabaztagLib.Choreography.ActivitySequence
        Get
            If _sequence Is Nothing Then
                _sequence = New NabaztagLib.Choreography.ActivitySequence
            End If
            Return _sequence
        End Get
        Set(ByVal value As NabaztagLib.Choreography.ActivitySequence)
            _sequence = value
            RebindControls()
        End Set
    End Property
#End Region

#Region "Methods"
    Public Sub RebindControls()
        TempoNumericUpDown.Value = Sequence.TempoInHertz

        ActivitiesFlowLayoutPanel.Controls.Clear()
        For Each activity As NabaztagLib.Choreography.IRabbitActivity In Sequence.Activities
            If activity.GetType Is GetType(NabaztagLib.Choreography.LedRabbitActivity) Then
                Dim ledControl As New LedActivityUserControl
                ledControl.Activity = activity
                AddHandler ledControl.RemoveLedActivity, AddressOf RabbitActivity_Remove
                ActivitiesFlowLayoutPanel.Controls.Add(ledControl)
            ElseIf activity.GetType Is GetType(NabaztagLib.Choreography.MotorRabbitActivity) Then
                Dim motorControl As New MotorActivityUserControl
                AddHandler motorControl.RemoveMotorActivity, AddressOf RabbitActivity_Remove
                motorControl.Activity = activity
                ActivitiesFlowLayoutPanel.Controls.Add(motorControl)
            End If
        Next
    End Sub
#End Region

#Region "Shared Methods"
    Private Shared Function CompareRabbitActivitiesByTime( _
        ByVal x As NabaztagLib.Choreography.IRabbitActivity, ByVal y As NabaztagLib.Choreography.IRabbitActivity) As Integer
        Dim retVal As Int32 = x.ActionMoment.CompareTo(y.ActionMoment)
        If retVal <> 0 Then
            Return retVal
        Else
            Return x.GetType.ToString.CompareTo(y.GetType.ToString)
        End If
    End Function
#End Region

#Region "Child Control Event Handlers"
    Private Sub SendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendButton.Click
        Dim message As New NabaztagLib.Messages.ChoreographyMessage
        message.ChoreographyTitle = TitleTextBox.Text
        message.SerialNumber = SerialNumberTextBox.Text
        message.Token = UInt32.Parse(TokenTextBox.Text)
        message.Sequence = Me.Sequence
        ResponseTextBox.Text = NabaztagLib.MessageService.SendMessage(message).ToString
    End Sub

    Private Sub AddMotorActivityButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddMotorActivityButton.Click
        Me.Sequence.Activities.Add(New NabaztagLib.Choreography.MotorRabbitActivity)
        RebindControls()
    End Sub

    Private Sub AddLedActivityButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddLedActivityButton.Click
        Me.Sequence.Activities.Add(New NabaztagLib.Choreography.LedRabbitActivity)
        RebindControls()
    End Sub

    Private Sub TempoNumericUpDown_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TempoNumericUpDown.ValueChanged
        Me.Sequence.TempoInHertz = TempoNumericUpDown.Value
    End Sub

    Private Sub ChoreographyUserControl_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.SerialNumberTextBox.Text = My.Settings.DefaultSerialNumber
        Me.TokenTextBox.Text = My.Settings.DefaultToken
    End Sub

    Private Sub SortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortButton.Click
        Me.Sequence.Activities.Sort(AddressOf CompareRabbitActivitiesByTime)
        RebindControls()
    End Sub

    Private Sub RabbitActivity_Remove(ByVal sender As Object, ByVal e As EventArgs)
        Dim control As IRabbitActivityControl = sender

        Sequence.Activities.Remove(control.Activity)
        RebindControls()
    End Sub
#End Region

End Class
